<#assign assign_TableName = content.content.tableName>
<#assign camelName = content.content.camelName>
<#assign assign_PascalName = content.content.pascalName>
<#assign assign_EntityName =content.items.entity.className>
<#assign assign_EntityFullName = content.items.entity.packageName+"."+content.items.entity.className>
<#assign tableName = content.content.tableName>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${content.items.dao.packageName}.${content.items.dao.className}">
	<#--判断日期类型和枚举类型的时候不要判 !=''，只判null即可，否则会报错，
		boolean类型的时候不要不要判 !=''，只判null即可，否则值为false时识别不到，但是此类型默认值是false，可改用Boolean,默认是null
		int类型，!=''判空，值为0不识别，且此类型默认值是0，可改用Integer，默认值是null
		-->
<#--	<!-- 实体类的返回map&ndash;&gt;
	<resultMap id="BaseResultMap" type="${assign_EntityFullName}">
	<#list content.content.fields as item>
		<#if item.primary == true>
		<id column="${item.name}" property="${item.fieldName}" />
		<#else>
		<result column="${item.name}" property="${item.fieldName}" />
		</#if>
	</#list>
	</resultMap>-->
	<!-- 数据库中表的列名 -->
	<sql id="Base_Column_List">
		<#list content.content.fields as item>t.${item.name}<#if item?has_next>,</#if></#list>
	</sql>

	<!-- 通过对象中不为空的属性作为条件获取数据 -->
	<select id="queryList" resultType="${assign_EntityFullName}">
		SELECT
			<include refid="Base_Column_List" />
		FROM ${assign_TableName} t
		<where>
		<#list content.content.fields as item>
			<#if item.primary != true>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
			</#if>
		</#list>
		<#list content.content.fields as item>
			<#if item.primary == true>
			<if test="${item.fieldName} != null and ${item.fieldName} != ''"> and t.${item.name} = ${r'#{'}${item.fieldName}${r'}'}</if>
			</#if>
		</#list>
		</where>
	</select>


<#if content.content.primaryField??>
	<#assign assign_idFieldType = content.content.primaryField[0].fieldType>
	<#assign assign_idFieldName = content.content.primaryField[0].fieldName>
	<#assign assign_idName = content.content.primaryField[0].name>
	<!-- 通过ID获取数据 -->
	<select id="getEntity" parameterType="${assign_idFieldType}" resultType="${assign_EntityFullName}">
		SELECT
			<include refid="Base_Column_List" />
		FROM ${assign_TableName} t
		WHERE t.${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</select>


	<!-- 通过IDs获取数据 -->
	<select id="getEntitys"  resultType="${assign_EntityFullName}">
		SELECT
			<include refid="Base_Column_List" />
		FROM ${assign_TableName} t
		WHERE t.${assign_idName} in
		<foreach collection="ids" index="index" item="item" open="(" separator="," close=")" >
			<#noparse>#{item}</#noparse>
		</foreach>
	</select>


	<!-- 插入一个对象,只插入对象中值不为null的属性 -->
	<insert id="save" parameterType="${assign_EntityFullName}">
		INSERT INTO ${assign_TableName}
		<trim prefix="(" suffix=")" suffixOverrides=",">
		<#list content.content.fields as item>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${item.name}<#if item?has_next>,</#if></if>
		</#list>
		</trim>
		<trim prefix="values (" suffix=")" suffixOverrides=",">
		<#list content.content.fields as item>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
		</#list>
		</trim>
	</insert>


	<!-- 更新一个对象中属性不为null值,条件为对象中的id -->
	<update id="update" parameterType="${assign_EntityFullName}">
		UPDATE ${assign_TableName}
		<set>
		<#list content.content.fields as item>
			<#if item.primary != true>
			<if test="${item.fieldName} != null<#if item.fieldType=="String"> and ${item.fieldName} != ''</#if>">${item.name} = ${r'#{'}${item.fieldName}${r'}'}<#if item?has_next>,</#if></if>
			</#if>
		</#list>
		</set>
		WHERE ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</update>


	<!-- 通过ID删除数据 -->
	<delete id="delete" parameterType="${assign_idFieldType}">
		DELETE FROM ${assign_TableName} WHERE ${assign_idName} = ${r'#{'}${assign_idFieldName}${r'}'}
	</delete>

</#if>
</mapper>